/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var NN = require('../Poker/NN');

var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');
var cheat = require('./random/getNiu.js')
exports.HSNNPlayer = PokerPlayer.extend({
    _robDealer: -1,             // 抢庄状态，-1/0/1、2、3、4... 未选择/不抢/抢庄
    _selectedCards: null,     // 选择的三张牌

    init: function (uid, msg) {
        this._super(uid, msg);

        this._robDealer = -1;
        this._selectedCards = null;
        this.showNiu = {

        };
        this._stat.roundPattern = Const.Pattern.NONE;
    },
    reset: function () {
        this._robDealer = -1;
        this._selectedCards = null;
    },
    setRotDealer: function (value) {
        this._robDealer = value;
    },

    getRobDealer: function () {
        return this._robDealer;
    },

    setSelectedCards: function (cardsArr) {
        if (!cardsArr || cardsArr.length == 0) {
            this._selectedCards = [];
            return true;
        }

        if (cardsArr.length != 3) {
            return false;
        }

        var cards = Poker.buildFromArray(cardsArr);
        var allExist = true;
        var allPoint = 0;

        ExObject.eachKeyNum(cards, function (card, num) {
            if (!card) {
                allExist = false;
            }

            if (this._handCards.getCardNum(card) < num) {
                allExist = false;
            }

            allPoint += NN.getPoint(card) * num;
        }.bind(this));

        if (!allExist) {
            return false;
        }

        if (allPoint % NN.BASE != 0) {
            return false;
        }

        this._selectedCards = [];
        cardsArr.forEach(function (card) {
            this._selectedCards.push(card);
        }.bind(this));

        return true;
    },

    isSelectedCards: function () {
        return (this._selectedCards) ? true : false;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function () {
        var info = this._super();
        info.remainScore = this._stat.remainScore;
        return info;
    },

    getReconnectInfo: function (isSelf) {
        var info = this._super(isSelf);

        info.robDealer = this._robDealer;

        if (isSelf) {
            info.selectCards = this._selectedCards;
            info.handCards = this._handCards.getCards();
        }
        info.score = this._stat.score;
        info.selectNum = (this._selectedCards) ? this._selectedCards.length : -1;

        return info;
    },

    getSettlementInfo: function () {
        var info = this._super();

        info.handCards = this._handCards.getInitCards();
        info.selectCards = this._selectedCards;
        info.showNiu = this.showNiu;
        return info;
    },

    settlementPattern: function () {
        if (!this._selectedCards) {
            var results = NN.calcNiuCards(this._handCards.getInitCards());
            this._selectedCards = (results.length > 0) ? results[0] : [];
        }

        this._calcNiu();
        if (!this.showNiu[this._stat.roundPattern]) {
            this.showNiu[this._stat.roundPattern] = 1;
        } else {
            this.showNiu[this._stat.roundPattern] += 1;
        }
    },

    settlement: function () {
        this._super();

        var room = this.getRoom();
        var player = this;

        //var roomMS = room.getOption("MS");
        //console.log(room.MS);
        if (room.MS == "NNSZ" || room.MS == "KPQZ") {
            var ePlayer = this.getRoom().getDealerPlayer();
            if (!ePlayer) {
                return;
            }

            if (this.uid() == ePlayer.uid()) {
                return;
            }
            var RobFB = 1;
            if (room.MS == "KPQZ" && ePlayer.getRobDealer() > 0) {
                RobFB = ePlayer.getRobDealer();
            }
            if(room.MS == "NNSZ" && player.getRobDealer() > 0){
                RobFB = player.getRobDealer();
            }
            if (ePlayer._stat.roundPattern > player._stat.roundPattern) {
                var score = room.baseScore * Const.PatternMulti[ePlayer._stat.roundPattern];
                score *= RobFB;
                //var score = Const.PatternScore[ePlayer._stat.roundPattern];
                ePlayer.addRoundScore(score);
                player.addRoundScore(-score);
            } else if (ePlayer._stat.roundPattern < player._stat.roundPattern) {
                var score = room.baseScore * Const.PatternMulti[player._stat.roundPattern];
                //var score = Const.PatternScore[player._stat.roundPattern];
                
                score *= RobFB;
                player.addRoundScore(score);
                ePlayer.addRoundScore(-score);
            } else {
                var score = room.baseScore * Const.PatternMulti[ePlayer._stat.roundPattern];
                score *= RobFB;
                //var score = Const.PatternScore[player._stat.roundPattern];
                
                var ePlayCard = ePlayer._getMaxCard();
                var playerCard = player._getMaxCard();
                if (NN.cardLT(ePlayCard, playerCard)) {
                    ePlayer.addRoundScore(score);
                    player.addRoundScore(-score);
                } else {
                    player.addRoundScore(score);
                    ePlayer.addRoundScore(-score);
                }
            }
        }
    },

    onStartNewRound: function () {
        this._super();
        //this._robDealer = -1;
        //this._selectedCards = null;
        this.reset();
        this._stat.roundPattern = Const.Pattern.NONE;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    /******************************************************************
     * 私有功能函数
     ******************************************************************/
    _calcNiu: function () {
        var room = this.getRoom();
        var selectCards = this._selectedCards;

        if (!selectCards || selectCards.length == 0) {
            var pattern = Const.Pattern.NONE;
            //炸弹牛
            var allCardsArr = this._handCards.getInitCards();
            var firstNum = 1;
            //var secondNum = 1;
            var first = Poker.getPoint(allCardsArr[0]);
            //var second = Poker.getPoint(allCardsArr[1]);
            for (var i = 1; i < allCardsArr.length; i++) {
                if (Poker.getPoint(allCardsArr[i]) == first) {
                    firstNum += 1;
                }
            }
            if (firstNum == 4) {
                if (room.specialCard["ZhaDanNiu"]) {
                    pattern = Const.Pattern.ZDN;
                    //return;
                }
            }
            if (firstNum == 1) {
                //var firstNum = 1;
                var secondNum = 1;
                //var first = Poker.getPoint(allCardsArr[0]);
                var second = Poker.getPoint(allCardsArr[1]);
                for (var i = 2; i < allCardsArr.length; i++) {
                    if (Poker.getPoint(allCardsArr[i]) == second) {
                        secondNum += 1;
                    }
                }

                if (secondNum == 4) {
                    if (room.specialCard["ZhaDanNiu"]) {
                        pattern = Const.Pattern.ZDN;
                        //return;
                    }
                }
            }

         

            //五小牛
            var flag = true;
            allCardsArr.forEach(function (card) {
                if (NN.getPoint(card) >= 5) {
                    flag = false;
                }
            });
            if (flag) {
                if (allCardsArr.reduce(function (a, b) {
                    return NN.getPoint(a) + NN.getPoint(b);
                }, 0) >= 10) {
                    flag = false;
                }
            }
            if (flag) {
                if (room.specialCard["WuXiaoNiu"]) {
                    pattern = Const.Pattern.WXN;

                } else {
                    //pattern = Const.Pattern.NN;
                }

            }
            if (pattern) {
                this._stat.roundPattern = pattern;

            }
            return
        }




        var allCardsArr = this._handCards.getInitCards();
        var otherPoints = 0;
        allCardsArr.forEach(function (card) {
            if (selectCards.indexOf(card) == -1) {
                otherPoints += NN.getPoint(card);
            }
        });

        var nPoint = otherPoints % NN.BASE;
        var pattern = Const.Pattern.NONE;

        if (nPoint == 0) {
            pattern = Const.Pattern.NN;
            var minPoint = Poker.Point.MAX;
            var maxPoint = Poker.Point.MIN;
            allCardsArr.forEach(function (card) {
                var point = Poker.getPoint(card);
                minPoint = Math.min(minPoint, point);
                maxPoint = Math.max(maxPoint, point);
            });

            if (minPoint >= Poker.Point.J && maxPoint <= Poker.Point.K) {
                pattern = Const.Pattern.WHN;
            }
            if (!room.specialCard["WuHuaNiu"]) {
                pattern = Const.Pattern.NN;
            }
        } else {
            pattern = Const.Pattern['N' + nPoint];
        }

        this._stat.roundPattern = pattern;
    },

    _getMaxCard: function () {
        var maxCard = 0;
        //var selectCards = this._selectedCards;
        var allCardsArr = this._handCards.getInitCards();

        allCardsArr.forEach(function (card) {

            if (!maxCard || NN.cardLT(card, maxCard)) {
                maxCard = card;
            }

        });

        return maxCard;
    },
    // getInitHandCardPreCards: function () {

    //     var preCards = [];
    //     if (this._prehandcards) {
    //         for (var i = 0; i < this._prehandcards.length; i++) {
    //             preCards.push(this._prehandcards[i]);
    //         }
    //     }
    //     var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
    //     console.log("distanceee")
    //     console.log(distance);
    //     if (distance == 0) {
    //         preCards.push(109);
    //         preCards.push(301);
    //         preCards.push(410);
    //         preCards.push(308);
    //         preCards.push(408);

    //     } else if (distance == 1 || distance == 4) {
    //         preCards.push(108);
    //         preCards.push(201);
    //         preCards.push(401);
    //         preCards.push(412);
    //         preCards.push(111);
    //     }else if (distance == 2 || distance == 5) {
    //         preCards.push(103);
    //         preCards.push(202);
    //         preCards.push(105);
    //         preCards.push(209);
    //         preCards.push(207);
    //     }
    //     var room = this.getRoom();
    //     logger.debug("room=%d noPreCards %j,cards = %j", room.rid(), preCards, this._prehandcards);

    //     return preCards;
    // },
    /**
     * 初始化手牌时默认牌
     * @returns {array}
     */
    getInitHandCardPreCards: function() {
        var controlValue = this.getUserValue("controlValue");
        if (controlValue) {
            var room = this.getRoom();
            var cards = [];
    
            if (controlValue >= 80) {
                // var looks = room.getPublicCards().lookCards(3);
    
                // cards.merge(looks);
                // cards.merge(looks);
                // cards.merge(looks);
                cards =cheat.getNiu(5)
            } else if (controlValue >= 60) {
                // var looks = room.getPublicCards().lookCards(2);
                // cards.merge(looks);
                // cards.merge(looks);
                // cards.merge(looks);
                cards = cheat.getNiu(4)
            } else if (controlValue >= 40) {
                // var looks = room.getPublicCards().lookCards(3);
                // cards.merge(looks);
                // cards.merge(looks);
                cards = cheat.getNiu(3)
            } else if (controlValue >= 20) {
                // var looks = room.getPublicCards().lookCards(2);
                // cards.merge(looks);
                // cards.merge(looks);
                cards = cheat.getNiu(2)
            } else if (controlValue >= 10) {
                cards = cheat.getNiu(1)
            }
    
            return cards;
        }
    },
});